﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

public static class TypeExtend
{
    public static DateTime AddTime(this DateTime source, string Time)
    {
        if (Time.Trim().Length != 4) throw new FormatException("時間格式不正確(Lenth)");
        int HH, mm;
        HH = Convert.ToInt32(Time.Substring(0, 2));
        mm = Convert.ToInt32(Time.Substring(2, 2));
        source = source.AddHours(HH);
        source = source.AddMinutes(mm);
        return source;
    }
    public static string TimeStringBy48HR(this DateTime source, DateTime AttendDate)
    {
        var ts = source.Date - AttendDate.Date;
        return ((ts.Days * 24 + source.Hour) * 100 + source.Minute).ToString("0000");
    }
    public static string SpanTimeString(this TimeSpan source, string Format)
    {
        string value = Format;

        DateTime baseDate = DateTime.MinValue + source;
        value = value.Replace("y", (baseDate.Year - 1).ToString());
        value = value.Replace("M", (baseDate.Month - 1).ToString());
        value = value.Replace("d", (baseDate.Day - 1).ToString());

        return value;
    }
    public static string ToYearMonthString(this decimal source, string Format = "y年M月")
    {
        string value = Format;

        int yy = Convert.ToInt32(decimal.Floor(source));
        int MM = Convert.ToInt32((source - Convert.ToDecimal(yy)) * 12);

        value = value.Replace("y", (yy).ToString());
        value = value.Replace("M", (MM).ToString());
        //value = value.Replace("d", (baseDate.Day - 1).ToString());

        return value;
    }
}

